package workflow

import (
	"cgs-server/helper"
	"cgs-server/server"
	"cgs-server/server/base"
	"cgs-server/server/model"
	"cgs-server/server/service"
	"cgs-server/server/store"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"net/http"
	"strconv"
)

type WorkflowHandler struct {
	// srv service
	srv service.Service
	// store
	store store.Factory
}

func NewWorkflowHandler(store store.Factory) *WorkflowHandler {
	return &WorkflowHandler{
		srv:   service.NewService(store),
		store: store,
	}
}

func (wf *WorkflowHandler) Get(w http.ResponseWriter, r *http.Request) {
	id := r.FormValue("id")
	if id == "" {
		helper.WriteJSON(w, server.Fail("id is required"))
		return
	}

	objId, err := primitive.ObjectIDFromHex(id)
	if err != nil {
		helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	user, _ := base.GetCurrentUser(r)

	workflow, err := wf.srv.Workflow().Get(&model.Workflow{ID: objId}, user)
	if err != nil {
		helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	helper.WriteJSON(w, server.OK().WithData(workflow))
}

func (wf *WorkflowHandler) Save(w http.ResponseWriter, r *http.Request) {
	workflow := &model.Workflow{}
	err := helper.ReadRequestBody(workflow, w, r)
	if err != nil {
		helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	user, _ := base.GetCurrentUser(r)
	err = wf.srv.Workflow().Save(workflow, user)
	if err != nil {
		helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	helper.WriteJSON(w, server.OK().WithData(bson.M{"id": workflow.GetId()}))
}

func (wf *WorkflowHandler) Update(w http.ResponseWriter, r *http.Request) {
	workflow := &model.Workflow{}
	err := helper.ReadRequestBody(workflow, w, r)
	if err != nil {
		helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	user, _ := base.GetCurrentUser(r)
	err = wf.srv.Workflow().Update(workflow, user)
	if err != nil {
		helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	helper.WriteJSON(w, server.OK())
}

func (wf *WorkflowHandler) Delete(w http.ResponseWriter, r *http.Request) {
	id := r.FormValue("id")
	if id == "" {
		helper.WriteJSON(w, server.Fail("id is required"))
		return
	}

	objId, err := primitive.ObjectIDFromHex(id)
	if err != nil {
		helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	user, _ := base.GetCurrentUser(r)

	err = wf.srv.Workflow().Delete(&model.Workflow{ID: objId}, user)
	if err != nil {
		helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	helper.WriteJSON(w, server.OK())
}

func (wf *WorkflowHandler) List(w http.ResponseWriter, r *http.Request) {
	category := r.FormValue("category")
	current, _ := strconv.ParseInt(r.FormValue("current"), 10, 64)
	size, _ := strconv.ParseInt(r.FormValue("size"), 10, 64)

	mdl := base.GenerateDataUserModel(&model.Workflow{
		Category: category,
	}, r)
	mdl.Page = &model.Page{
		Current: current,
		Size: size,
	}
	mdl.ResolvePageOption()

	workflows, err := wf.srv.Workflow().List(mdl)
	if err != nil {
		helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	helper.WriteJSON(w, server.OK().WithData(workflows))

}
